package sdu.attendancechecker.servlet.teacher;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.catalina.connector.Request;

import sdu.attendancechecker.dto.Attandance;
import sdu.attendancechecker.dto.Group;
import sdu.attendancechecker.dto.Schedule;
import sdu.attendancechecker.dto.Student;
import sdu.attendancechecker.dto.Subject;
import sdu.attendancechecker.dto.Teacher;
import sdu.attendancechecker.models.ScheduleModel;
import sdu.attendancechecker.models.TeacherModel;
import sdu.attendancechecker.models.SubjectModel;
import sdu.attendancechecker.services.TeacherService;

public class GroupServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		Teacher t = (Teacher) session.getAttribute("teacher");
		if (t == null) {
			response.sendRedirect("teacher/login");
			return;
		}
		// Date date = new Date();
		// System.out.println("Date = " + date.getDay());
		// date.setDate(date.getDate() - 6);
		// System.out.println("Date after = " + date.getDay());

		if (request.getParameter("subject") != null
				&& request.getParameter("group") == null) {
			String subject = request.getParameter("subject");
			ArrayList<Integer> studentids = SubjectModel
					.getStudentsByTeacherAndClass(t.getId(),
							subject);
			ArrayList<Group> groups = SubjectModel
					.getGroupsByStudents(studentids);
			session.setAttribute("groups", groups);
			session.setAttribute("subjectid", subject);
			
			session.setAttribute("subjectname", "");
			for (Subject x : SubjectModel.getSubjects(t.getId())) {
				if (x.getId().equals(subject)) {
					session.setAttribute("subjectname", x.getName());
					break;
				}
			}
			response.sendRedirect("../teacher/groups");
		}

		if (request.getParameter("group") != null) {
			String id = request.getParameter("subject");
			String groupId = request.getParameter("group");
			System.out.println("kirid");
			
			//
			request.setAttribute("subjectname", "");
			for (Subject x : SubjectModel.getSubjects(t.getId())) {
				if (x.getId().equals(id)) {
					request.setAttribute("subjectname", x.getName());
					break;
				}
			}
			//
			
			ArrayList<Group> groups = (ArrayList<Group>) session
					.getAttribute("groups");
			ArrayList<Attandance> attendance = null;
			for (int i = 0; i < groups.size(); i++) {
				if (groups.get(i).getName().equals(groupId)) {
					attendance = SubjectModel.getAttendance(groups.get(i)
							.getStudents(), id);
					break;
				}
			}
			ArrayList<String> dates = new ArrayList<String>();
			Date date = new Date();
			SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-dd");
			ArrayList<Schedule> schdl = ScheduleModel
					.getSchedule(id, t.getId());
			int maxDate = 5;
			while (dates.size() < maxDate) {
				for (Schedule x : schdl) {
					if (x.getWeek() == (date.getDay() == 0 ? 7 : date.getDay())) {
						dates.add(0, sdf.format(date));
					}
					if (dates.size() >= maxDate)
						break;
				}
				date.setDate(date.getDate() - 1);
			}
			ArrayList<Attandance> att2 = new ArrayList<Attandance>();
			for (int i = 0; i < attendance.size(); i++) {
				Attandance ta = attendance.get(i);
				att2.add(new Attandance(ta.getStudent(), dates,
						new ArrayList<String>(), new ArrayList<Integer>(),
						new ArrayList<Integer>()));
			}
			for (String x : dates) {
				for (int i = 0; i < attendance.size(); i++) {
					Attandance ta = attendance.get(i);
					boolean flag = false;
					for (int j = 0; j < ta.getDates().size(); j++) {
						if (x.equals(ta.getDates().get(j))) {
							int att = ta.getAtt().get(j);
							att2.get(i).getAtt().add(att);
							flag = true;
							break;
						}
					}
					if (!flag) {
						att2.get(i).getAtt().add(0);
					}
				}
			}
			// System.out.println(dates.toString());
			// System.out
			// .println(getServletContext().getRealPath("/") + "excel\\");
			// System.out.println(request.getRealPath("/"));
			// String path = "C:\\Java\\Excel\\";
			if (request.getParameter("upload") != null) {
				TeacherService.export(dates, att2, response);
				return;
			}
			session.setAttribute("dates", dates);
			session.setAttribute("attendance", att2);
			// response.sendRedirect("../teacher/group");
			RequestDispatcher disp = request.getRequestDispatcher("group");
			disp.forward(request, response);
			return;
		}
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

	}
}
